package com.anbinh.doctor.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.fragment.benhnhan.FragmentListPatients;
import com.example.stickyheadergridview.GridItem;

public class HistoryMedicalAdapter extends BaseAdapter {

	private static final int TYPE_ITEM = 0;
	public static final int TYPE_SEPARATOR = 1;
	private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

	private LayoutInflater mInflater;
	private Context context;
	private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
	private List<GridItem> mData = new ArrayList<GridItem>();

	public HistoryMedicalAdapter(Context mContext) {
		mInflater = LayoutInflater.from(mContext);
		context = mContext;
	}

	public void addItem(final GridItem item) {
		mData.add(item);
		notifyDataSetChanged();
	}

	public void removeItem(int position) {
		mData.remove(position);
		notifyDataSetChanged();
	}

	public void RemoveData() {
		mData.removeAll(mData);
		mSeparatorsSet.removeAll(mSeparatorsSet);
		notifyDataSetChanged();
	}

	public void addSeparatorItem(final GridItem item) {
		mData.add(item);
		mSeparatorsSet.add(mData.size() - 1);
		notifyDataSetChanged();
	}

	@Override
	public int getItemViewType(int position) {
		return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
	}

	@Override
	public int getViewTypeCount() {
		return TYPE_MAX_COUNT;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public GridItem getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		int type = getItemViewType(position);
		if (convertView == null) {
			holder = new ViewHolder();
			switch (type) {
			case TYPE_SEPARATOR:

				convertView = mInflater.inflate(R.layout.hedader_lich_kham_bn,
						null);
				holder.lblTitle = (TextView) convertView
						.findViewById(R.id.lblTitle);

				break;

			case TYPE_ITEM:

				convertView = mInflater.inflate(R.layout.item_medical_history,
						null);
				holder.lblName = (TextView) convertView
						.findViewById(R.id.lblName);
				holder.lbltime = (TextView) convertView
						.findViewById(R.id.lbltime);
				holder.lblTrangthai = (TextView) convertView
						.findViewById(R.id.lblTrangthai);
				holder.lblAddress = (TextView) convertView
						.findViewById(R.id.lblAddress);

				break;
			}
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final GridItem item = getItem(position);
		if (type == TYPE_SEPARATOR) {
			holder.lblTitle.setText(item.getGroupName());
		} else {
			holder.lblName.setText(FragmentListPatients.newInstance()
					.getCurrentPatient().getPatient_name());
			holder.lbltime.setText(item.getSchedules().getStart_time());
			holder.lblTrangthai.setText(item.getSchedules().getNameStatus());
			holder.lblAddress.setText(item.getGroupAddress());
		}

		return convertView;
	}

	public static class ViewHolder {
		public TextView lblAddress;
		public TextView lblTrangthai;
		public TextView lblTitle;
		public TextView lblTitle_add;

		public ImageView thumnail;
		public TextView lblName;
		public TextView lbltime;
		public TextView lblThoigian;
		public LinearLayout btnSetup;
		public LinearLayout lineMore;
	}

}